package com.zoweunion.mechanic.service.base.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zoweunion.mechanic.util.*;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zoweunion.mechanic.dao.base.TuiHuanDao;
import com.zoweunion.mechanic.service.base.TuiHuanService;

@Service
@Transactional
public class TuiHuanServiceImpl implements TuiHuanService {

	@Autowired
	private TuiHuanDao tuiHuanDao;

	@Override
	public int applyTuiHuan(Map<String, Object> reqMap) throws MyException{

		//生成主键uuid
		reqMap.put("id", UuidUtil.get32UUID().toString());
//		reqMap.put("return_type",Integer.valueOf(reqMap.get("return_type").toString())-0);
//		reqMap.put("quantity",Integer.parseInt(reqMap.get("quantity").toString()));
		int i = 0;
		try {
			i = tuiHuanDao.applyTuiHuan(reqMap);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (i != 1) {
			
			throw new MyException(Constants.CODE_HANDEL_FAILED, "业务受理失败");
		}

		return i;
	}

	@Override
	public int applyPurchase(Map<String, Object> reqMap) throws MyException {

		//生成主键uuid
		reqMap.put("id", UuidUtil.get32UUID().toString());
		//根据服务商id查询服务商名称
//		String s_name = tuiHuanDao.getSnameBySid(reqMap);
		//将服务商名放入repMap中
//		reqMap.put("s_name",s_name);
		//将服务商申请记录插入相关的表中
		int i = 0;
		try {
			i = tuiHuanDao.applyPurchase(reqMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (i != 1) {
			throw new MyException(Constants.CODE_HANDEL_FAILED, "业务受理失败");
		}
		return i;
		
	}

	@Override
	public Map<String, Object> getPurchaseByCondition(Map<String, Object> reqMap) throws MyException {
		//根据条件模糊查询设备采购列表
		reqMap.put("currentPage",(Integer.valueOf(reqMap.get("currentPage").toString())-1)*Integer.valueOf(reqMap.get("pageSize").toString()));
		if(!reqMap.get("startTime").equals("")){
			reqMap.put("startTime", DateUtil.utcToLocal(reqMap.get("startTime").toString()));
			reqMap.put("endTime",DateUtil.utcToLocalAddOne(reqMap.get("endTime").toString()));
		}
		int	total = tuiHuanDao.getPurchaseByCount(reqMap);
		List<Map<String, Object>> list = tuiHuanDao.getPurchaseByCondition(reqMap);
		// 包装结果集 总条数 总页数 数据集
//		int totalPage = total%Integer.valueOf(reqMap.get("pageSize").toString()) == 0 ? total/Integer.valueOf(reqMap.get("pageSize").toString()) : (total/Integer.valueOf(reqMap.get("pageSize").toString())+1);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("total", total);//总数
		resultMap.put("list", list);
		return resultMap;
		
	}

	@Override
	public Map<String,Object> purchaseName(Map<String, Object> reqMap) {
		Map<String,Object> result =tuiHuanDao.getSnameBySid(reqMap);
		return result;
	}

	@Override
	public Map<String, Object> getApplyTuiHuanByCondition(Map<String, Object> reqMap) {
		//根据条件模糊查询设备采购列表
		reqMap.put("currentPage",(Integer.valueOf(reqMap.get("currentPage").toString())-1)*Integer.valueOf(reqMap.get("pageSize").toString()));
		if(!reqMap.get("startTime").equals("")){
			reqMap.put("startTime", DateUtil.utcToLocal(reqMap.get("startTime").toString()));
			reqMap.put("endTime",DateUtil.utcToLocalAddOne(reqMap.get("endTime").toString()));
		}
		int	total = tuiHuanDao.getTuiHuanByCount(reqMap);
		List<Map<String, Object>> list = tuiHuanDao.getTuiHuanByCondition(reqMap);
		// 包装结果集 总条数 总页数 数据集
//		int totalPage = total%Integer.valueOf(reqMap.get("pageSize").toString()) == 0 ? total/Integer.valueOf(reqMap.get("pageSize").toString()) : (total/Integer.valueOf(reqMap.get("pageSize").toString())+1);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("total", total);//总数
		resultMap.put("list", list);
		return resultMap;
	}


}
